/*
* @author : TF-BJ-C064
* @creation : 2014-6-27 下午4:30:37
* @description : 
*
*/

package com.action.park;

import java.io.PrintWriter;
import java.sql.Timestamp;
import java.text.SimpleDateFormat;
import java.util.List;

import org.apache.log4j.Logger;

import com.base.UserBean;
import com.bean.ParkManager;
import com.bean.ParkOrder;
import com.bean.Parkinfo;
import com.bean.PerformanceBean;
import com.dao.ParkOrderDao;
import com.service.ParkOrderService;
import com.service.ParkinfoService;
import com.util.GUtil;

public class Performance extends BaseParkAction{

	private static final Logger log = Logger.getLogger(ParkOrderDao.class);
	 
	ParkOrderService service;
	ParkinfoService parkService;
	ParkOrder parkOrder;
	List<ParkOrder> list;
	List<PerformanceBean> listPF;
	List<Parkinfo> listPark;
	Parkinfo parkInfo;
	ParkManager parkManager;
	String datetime;

	Boolean queryMyOrder=true;//true: 收费员查询自己的订单 ; false查询所有订单
	Timestamp t;
	@Override
	public String handle() throws Exception {
		this.setHeaderTitle("绩效统计");
		//获取自己的停车场列表
		parkuser = (ParkManager) session.get("parkuser");
		listPark = parkService.getMyParkArea(parkuser);
		
		return GUtil.SUCCESS;
	}

	/**
	 * @return  绩效统计
	 * @throws Exception
	 * @see com.action.park.BaseParkAction#query()
	 */
	@Override
	public String query() throws Exception {
		this.setHeaderTitle("绩效统计");
		if(datetime==null){
			this.setMsg("请选择月份");
			return GUtil.SUCCESS;
		}
		if(user.getRole()==3){// 停车场超级管理员  
			
			//获取自己的停车场列表
			parkuser = (ParkManager) session.get("parkuser");
			listPark = parkService.getMyParkArea(parkuser);
			
			//查看本停车场所有的订单
		//	list  = service.queryMyParkOrder(parkOrder, user, page);
			listPF = service.statisMyParkPerformance( listPark, parkManager, parkInfo, datetime, page);//绩效统计
			session.put("listPF", listPF);// for exportExcel
			session.put("datetime", datetime);// for exportExcel
			if(listPF==null){
				log.info("停车场管理员"+user.getUserCode()+" - "+user.getUserName()+"  查看绩效统计 listPF==null");
			}else{
				log.info("停车场管理员"+user.getUserCode()+" - "+user.getUserName()+"  查看绩效统计 listPF.size= "+listPF.size());
			}
		}else if(user.getRole()==1){
			//停车场收费员
			if(queryMyOrder){//只查看我的订单
				list  = service.queryMyOrder(parkOrder, user, page);
			}
		}else if(user.getRole()>UserBean.MAX_PARK_ROLE_ID){
			//后台管理员
			list = service.query(parkOrder, user, page);//查看所有订单
			
			
		}
		return GUtil.SUCCESS;
	}
	
	/**
	 * 导出绩效信息(excel)
	 * 
	 * @return
	 */
	String title;
	public String exportExcel() throws Exception{
		String data = request.getParameter("data");
		
		listPF = (List<PerformanceBean>) session.get("listPF");// for exportExcel
		datetime = (String) session.get("datetime");// for exportExcel
		if(listPF==null){
			this.setMsg("暂无数据");
			return GUtil.SUCCESS;
		}
		title = datetime + "绩效统计";
		String html = "<div>";
		html += "<table style=\"width:800px;\">";
		html += "<tr><td colspan=7 ><center> <h2>"+title+"<h2> </center> </td></tr>";
		html += "<tr><td>序号</td><td>收费员帐号</td><td>姓名</td><td>订单总数</td><td>应收入总额</td><td>实收总额</td><td>比例</td></tr>";
		int i=0;
		for(PerformanceBean pf :listPF){
			i++;
			html += "<tr>";
			html += "	<td>"+i+"</td>";
			html += "	<td>"+pf.getUsercode()+"</td>";
			html += "	<td>"+pf.getUsername()+"</td>";
			html += "	<td>"+pf.getOrderSum()+"</td>";
			html += "	<td>"+pf.getReAmount()+"</td>";
			html += "	<td>"+pf.getAcAmount()+"</td>";
			html += "	<td>"+pf.getPercent()+"</td>";
			html += "</tr>";
		}
		html += "</table>";
		html += "</div>";
		request.setAttribute("data", html);
		String url = "/modules/report/excel.jsp?title="+title;
		request.getRequestDispatcher(url).forward(request, response);
		return GUtil.NONE;
	}
	/**
	 * 拆分字符串(将"A#B#C#D#E#F#"格式的字符串转换为"A,B,C,D,E,F"),并输出	 
	 * */
	public static String charReplacement(String sourceStr, String sourceChar,
			String targetChar) {
		String result = "";

		// 将需要处理的字符串中含有的sourceChar,全部替换为targetChar
		String temp = sourceStr.replaceAll(sourceChar, targetChar);

		// 截取字符串中最后一个字符;
		int len = temp.length();
		result = temp.substring(0, len - 1);
		return result;
	}
	@Override
	public String delete() throws Exception {
		return null;
	}
	@Override
	public String view() throws Exception {
		if(id==null)
			return GUtil.ERROR;
		parkOrder =(ParkOrder) service.get(Long.valueOf(id));
		return GUtil.VIEW;
	}

	@Override
	public String add() throws Exception {
		return null;
	}

	@Override
	public String update() throws Exception {
		return null;
	}
	/*
	 * 导出excel
	 */
	public String export() throws Exception {
		
		return null;
	}
	/**
	 * @return the parkOrder
	 */
	public ParkOrder getParkOrder() {
		return parkOrder;
	}

	/**
	 * @param parkOrder the parkOrder to set
	 */
	public void setParkOrder(ParkOrder parkOrder) {
		this.parkOrder = parkOrder;
	}

	/**
	 * @return the list
	 */
	public List<ParkOrder> getList() {
		return list;
	}

	/**
	 * @param list the list to set
	 */
	public void setList(List<ParkOrder> list) {
		this.list = list;
	}

	/**
	 * @return the queryMyOrder
	 */
	public Boolean getQueryMyOrder() {
		return queryMyOrder;
	}

	/**
	 * @param queryMyOrder the queryMyOrder to set
	 */
	public void setQueryMyOrder(Boolean queryMyOrder) {
		this.queryMyOrder = queryMyOrder;
	}

	public ParkOrderService getService() {
		return service;
	}

	public void setService(ParkOrderService service) {
		this.service = service;
	}

	public Timestamp getT() {
		return t;
	}

	public void setT(Timestamp t) {
		this.t = t;
	}


	public String getDatetime() {
		return datetime;
	}

	public void setDatetime(String datetime) {
		this.datetime = datetime;
	}


	@Override
	public boolean validate(Object obj) throws Exception {
		return false;
	}

	public List<Parkinfo> getListPark() {
		return listPark;
	}

	public void setListPark(List<Parkinfo> listPark) {
		this.listPark = listPark;
	}
	public ParkinfoService getParkService() {
		return parkService;
	}

	public void setParkService(ParkinfoService parkService) {
		this.parkService = parkService;
	}

	public ParkManager getParkManager() {
		return parkManager;
	}

	public void setParkManager(ParkManager parkManager) {
		this.parkManager = parkManager;
	}

	public Parkinfo getParkInfo() {
		return parkInfo;
	}

	public void setParkInfo(Parkinfo parkInfo) {
		this.parkInfo = parkInfo;
	}

	public List<PerformanceBean> getListPF() {
		return listPF;
	}

	public void setListPF(List<PerformanceBean> listPF) {
		this.listPF = listPF;
	}

	public String getTitle() {
		return title;
	}

	public void setTitle(String title) {
		this.title = title;
	}
}
